package base.dao;

import base.bean.Seckill;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by 30509 on 2018/3/25.
 */
@Repository
public interface SeckillDao extends JpaRepository<Seckill,Integer> {
    /**
     * 减库存
     * @param seckillId
     * @param killTime
     * @return 如果影响行数>1，标识更新的记录行数
     */
    @Modifying
    @Transactional
    @Query(value = "update seckill set number = number-1 where seckill_id = ?1 and " +
            "start_time <= ?2 and end_time >= ?2 and number > 0", nativeQuery = true)
    int reduceNumber (long seckillId,Date killTime);


    /**
     * 根据ID查询秒杀对象
     * @param seckillId
     * @return
     */
    @Query(value = "select seckill_id,name,number,start_time,end_time,create_time from seckill where seckill_id = ?1",
            nativeQuery = true)
    Seckill queryById(long seckillId);

    /**
     * 根据偏移量查询秒杀商品列表
     * @param offet
     * @param limit
     * @return
     */
    @Query(value = "select seckill_id,name,number,start_time,end_time,create_time from seckill order by create_time desc limit ?1,?2",nativeQuery = true)
    List<Seckill> queryAll(int offet,int limit);


}
